﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Acms.Bll;

public partial class Posts_Create : BasePostPage
{
    public override Role.Roles[] AllowedRoles
    {
        get { return new Role.Roles[]{Role.Roles.Developer, Role.Roles.ContentEditor, Role.Roles.ContentPublisher}; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (CurrentPost == null)
            {
                pnlParent.Visible = false;
                //check there is no site root
                Post root = Post.GetTopLevelBySiteId(AcmsContext.SiteId, null);
                if (root != null)
                {
                    pnlInsert.Visible = false;
                    pnlNoParent.Visible = false;
                    pnlRootExists.Visible = false;
                    pnlNoTemplate.Visible = false;
                }
                this.Title = "Create Home Page";
            }
            else
            {
                pnlParent.Visible = true;
                pnlInsert.Visible = true;
                pnlNoParent.Visible = false;
                pnlRootExists.Visible = false;
                pnlNoTemplate.Visible = false;

                lblParentName.Text = CurrentPost.DisplayName;
                lblParentPath.Text = CurrentPost.Path;

                parentGuid = CurrentPost.PostGuid;
                parentTemplateId = CurrentPost.TemplateId;
                this.Title = "Create Child Post";
            }
        }
    }

    protected override string PageTitle
    {
        get { return "Create"; }
    }


    Guid? parentGuid;
    int? parentTemplateId;

    protected void dsTemplates_SelectData(object sender, Acms.DataControls.SelectEventArgs<Template> e)
    {
        if (parentTemplateId.HasValue)
        {
            List<Template> tmp = Template.GetAllByParentTemplateId(parentTemplateId.Value);
            if(tmp.Count == 0)
            {
                pnlNoTemplate.Visible = true;
                pnlInsert.Visible = false;
            }
            e.SetData(tmp);
        }
        else
        {
            e.SetData(Template.GetAllBySiteId(AcmsContext.SiteId));
        }
    }

    protected void dvCreate_DataBound(object sender, EventArgs e)
    {
        if (parentGuid == null)
        {
            TextBox displayName = dvCreate.Rows[0].Cells[1].Controls[0] as TextBox;
            TextBox name = dvCreate.Rows[1].Cells[1].Controls[0] as TextBox;
            if (displayName != null)
            {
                displayName.Text = "Home Page";
            }
            if (name != null)
            {
                name.Enabled = false;
                name.Text = "~";
            }
        }
    }
    protected void dsPost_UpdatingData(object sender, Acms.DataControls.UpdatingDataEventArgs<Post> e)
    {
        if (e.Action == Acms.DataControls.UpdateAction.Insert)
        {
            if (string.IsNullOrEmpty(e.Item.DisplayName))
            {
                e.Item.DisplayName = e.Item.Name;
            }

            e.Item.SiteId = AcmsContext.SiteId;
            e.Item.ParentGuid = parentGuid;
            e.Item.RequiresPublishing = true;
        }
    }

    protected void dvCreate_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
    {
        int id = (int)e.Values["Id"];
        Response.Redirect(string.Concat("Details.aspx?Id=", id));
    }

    protected override void LoadViewState(object savedState)
    {
        base.LoadViewState(savedState);
        parentGuid = ViewState["ParentGuid"] as Guid?;
        parentTemplateId = ViewState["ParentTemplateId"] as int?;
    }

    protected override object SaveViewState()
    {
        ViewState["ParentGuid"] = parentGuid;
        ViewState["ParentTemplateId"] = parentTemplateId;
        return base.SaveViewState();
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        dvCreate.InsertItem(true);
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        string url = Request.QueryString["ReturnUrl"];
        if (string.IsNullOrEmpty(url))
        {
            url = "~/Posts/Default2.aspx";
        }
        Server.Transfer(url, false);
    }
}
